import sys
sys.path.append(r"/data/finance.Source/collectionData/utils")
sys.path.append(r"/data/finance.Source/collectionData/getKLinesdata")

import sqlutils
import getDailyKLine



#  插入数据库
def insetsql(r_lists,code):
    connection = sqlutils.connect(dbname='futures')
    try:
        with connection.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `dailykline` (`date`,`open`,`high`,`low`,`close`,`vol`,`code`) VALUES (%s, %s, %s, %s, %s, %s, '" + code + "')"
            for r_list in r_lists:
                print(r_list)
                if r_list[0] > '2019-04-03':
                        print(r_list)
                        cursor.execute(sql, (r_list))
#                cursor.execute(sql, (r_list))
            connection.commit()
    finally:
            connection.close()

#insetsql(r_lists)

 # RB0 螺纹钢   AG0 白银 AU0 黄金 CU0 沪铜 AL0 沪铝 ZN0 沪锌 PB0 沪铅 RU0 橡胶 FU0 燃油 WR0 线材 A0 大豆 M0 豆粕 Y0 豆油 J0 焦炭
 # C0 玉米 L0 乙烯 P0 棕油 V0 PVC RS0 菜籽 RM0 菜粕 FG0 玻璃 CF0 棉花 WS0 强麦 ER0 籼稻 ME0 甲醇 RO0 菜油 TA0 甲酸
# 取新浪数据接口   , 
def getXinLandata():
#    code_list = ['FG1905','FG1909']
    code_list = ['a1905','a1909','a1911','a2001','b1905','b1909','b1911','b2001','c1905','c1909','c1911','c2001','cs1905','cs1909','cs1911','cs2001','eg1906','eg1909','eg1912','fb1905','i1905','i1909','i1911','j1905','j1909','j1911','jm1905','jm1909','jm1911','y1905','y1909','y1911','ag1906','ag1909','ag1912','rb1905','rb1910','hc1905','hc1910','ru1905','ru1909','cu1904','cu1905','cu1906','cu1907','cu1908','cu1909','cu1910','cu1911','cu1912','CF1905','CF1909','FG1905','FG1909','SR1905','SR1909','ZC1905','ZC1907','ZC1907','ZC1907','TA1905','TA1907','TA1909','TA1911']
    for code in code_list:
        print(code)
        r_lists = getDailyKLine.getData(code)
        insetsql(r_lists,code)


getXinLandata()
